package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Brand;
import com.abaaba.project2.bean.BrandExample;
import com.abaaba.project2.bean.vo.BrandPageListVo;
import com.abaaba.project2.bean.vo.BrandPageVo;
import com.abaaba.project2.mapper.BrandMapper;
import com.abaaba.project2.service.WxBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxBrandServiceImpl implements WxBrandService {
    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandPageVo queryBrand(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        List<BrandPageListVo> brandPageListVos = new ArrayList<>();
        for (Brand brand : brands) {
            BrandPageListVo brandPageListVo = new BrandPageListVo();
            brandPageListVo.setId(brand.getId());
            brandPageListVo.setDesc(brand.getDesc());
            brandPageListVo.setFloorPrice(brand.getFloorPrice());
            brandPageListVo.setName(brand.getName());
            brandPageListVo.setPicUrl(brand.getPicUrl());
            brandPageListVos.add(brandPageListVo);
        }


        BrandPageVo brandPageVo = new BrandPageVo();
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        brandPageVo.setBrandList(brandPageListVos);
        brandPageVo.setTotalPages(brandPageInfo.getTotal());
        return brandPageVo;
    }

    @Override
    public Brand queryBrandDetail(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }
}
